<?php
namespace app\controller;

use app\BaseController;
use app\model\user\UserModel;
use app\facade\Str;
use app\facade\Token;

class Auth extends BaseController
{
    public function login() {
        $username = input('post.username');
        $password = input('post.password');

        if ($username == '' || $password == '') {
            return json([
                'msg'   => '错误',
            ]);
        }

        // 判断用户名是否存在
        $U = UserModel::where("username",$username)->find();
        if ($U == null) {
            return json([
                'msg'   => "用户不存在！~"
            ]);
        }
        if ($U['password'] != (Str::code($password,'E',config('key.password')))) {
            return json([
                'msg'   => "密码错误~"
            ]);
        }

        // OK啦
        $uid = $U['uid'];
        $token = Token::create($uid);
        
        cookie("token",$token,864000);
        cookie("uid",$uid,864000);

        return json([
            "uid"   => $U['uid'],
            'token' => $token,
            'code'  => 0
        ]);
    }
}